<template>
  <div class="login">
    <div class="login_box">
      <div class="logo_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="form_box">
        <el-form status-icon :model="form.data" :rules="form.rules" ref="form">
          <el-form-item prop="username">
            <el-input
              type="text"
              v-model="form.data.username"
              prefix-icon="el-icon-user-solid"
              placeholder="请输入用户名"
              @keyup.enter.native="submit"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="form.data.password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              @keyup.enter.native="submit"
            ></el-input>
          </el-form-item>
          <el-form-item class="btn_box">
            <el-button type="primary" @click="submit">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        data: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 4, max: 12, message: '用户名长度4~12位', trigger: 'blur' },
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 12, message: '密码长度6~12位', trigger: 'blur' },
          ],
        },
      },
    };
  },
  methods: {
    reset() {
      // this.$refs.form创建表单的实例对象
      this.$refs.form.resetFields();
    },
    submit() {
      // 在提交之前，要先判断一下表单数据是否合法
      // this.$refs.form
      //   .validate()
      //   .then(() => {
      //     return axios({
      //       method: 'POST',
      //       url: 'http://127.0.0.1:8888/api/private/v1/login',
      //       data: this.form.data,
      //     });
      //   })
      //   .then((res) => {
      //     if (res.data.meta.status === 200) {
      //       // this.$router.push('/');
      //       console.log(res);
      //       localStorage.setItem('token', res.data.data.token);
      //     } else {
      //       console.log(res.data.meta.msg);
      //     }
      //   });

      this.$refs.form
        .validate()
        .then(() => {
          return this.request.login(this.form.data);
        })
        .then((res) => {
          if (res.data.meta.status === 200) {
            // console.log(res);
            localStorage.setItem('token', res.data.data.token);
            this.$router.push('/');
            this.$message({
              message: '登录成功！',
              type: 'success',
            });
          } else {
            // console.log(res.data.meta.msg);
            this.$message.error('用户名或密码错误！');
          }
        });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    position: relative;
    width: 450px;
    height: 304px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: #fff 0 0 5px 1px;
    .logo_box {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      overflow: hidden;
      width: 130px;
      height: 130px;
      padding: 10px;
      border-radius: 50%;
      box-shadow: #ddd 0 0 5px 1px;
      background-color: #fff;
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #efefef;
      }
    }
    .form_box {
      padding: 20px;
      position: absolute;
      bottom: 0;
      width: 100%;
      box-sizing: border-box;
      .btn_box {
        margin-bottom: 0;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
